package com.yourblog.jerry_springboot.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yourblog.jerry_springboot.vo.FileVo;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @ClassName testcontroller
 * @Author WYQ
 * @Date 2020-10-30 16:36
 * @Version1.0
 **/
@CrossOrigin
@RestController
@RequestMapping("test")
public class testcontroller {
    //测试用
    String filePath = "D:\\file\\createFileDemo.txt";
    String dirPath = "D:\\file\\video";

    @RequestMapping("refresh")
    public void refresh(@RequestParam String[] rightData) {
        String filePath = "D:\\file\\"+"listDict.txt";
        List<String> list = new ArrayList<>();
        for (String str:rightData) {
            list.add(str);
        }
        writeToFile(filePath,list);
    }

    //向子目录分配文件
    @RequestMapping("add")
    public void add(@RequestParam String[] rightData, @RequestParam String fileName){
        String filePath = "D:\\file\\"+fileName;
        List<String> list = new ArrayList<>();
        for (String str:rightData) {
            list.add(str);
        }
        writeToFile(filePath,list);
    }

    @RequestMapping("listDict")
    public Object listDict(){
        String filePath = "D:\\file\\listDict.txt";
        List<String> list = getDataByFileName(filePath);
        List<FileVo> fileVos = new ArrayList<>();
        int i = 0;
        for (String str: list) {
            FileVo fileVo = new FileVo();
            fileVo.setIndex(i++);
            fileVo.setFilename(str);
            fileVos.add(fileVo);
        }
        return fileVos;
    }

    /**
     * 1.遍历txt获得所有文件列表
     * 2.txt文件中的列表排序
     * @throws IOException
     */

    /**
     * 遍历文件夹获得所有文件名
     */
    @RequestMapping("allFileDict")
    public Object allFileDict(){
        /* 获取文件夹路径 */
        String dirPath = "D:\\file\\video";
        List<String> list = getAllFileNameByDir(dirPath);
        List<FileVo> fileVos = new ArrayList<>();
        int i = 0;
        for (String str: list) {
            FileVo fileVo = new FileVo();
            fileVo.setIndex(i++);
            fileVo.setFilename(str);
            fileVos.add(fileVo);
        }
        return fileVos;
    }

    /**
     * 创建文件
     * @throws IOException
     */
    @RequestMapping("creatTxt")
    public Object creatTxt(@RequestParam String fileName){
        String filePath = "D:\\file\\"+fileName;
        boolean staut = createFile(filePath);
        return staut;
    }

    /**
     * 列表重新加入listDict
     * @param json
     * @return
     * @throws Exception
     */
    @RequestMapping("listDictAdd")
    public Object listDictAdd(@RequestBody String json){
        /* 反序列化 */
        List<FileVo> fileVos= JSONArray.parseArray(json, FileVo.class);
        String filePath = "D:\\file\\listDict.txt";
        // 遍历获取对象数组中的Filename的值
        List<String> nameList = fileVos.stream().map(FileVo -> FileVo.getFilename()).collect(Collectors.toList());
        writeToFile(filePath,nameList);
        return true;
    }

    /**
     * 删除文件
     * @param fileName
     * @return
     */
    @RequestMapping("delTxt")
    public Object delTxt(@RequestParam String fileName){
        String filePath = "D:\\file\\"+fileName;
        boolean staut = delFile(filePath);
        return staut;
    }

    @RequestMapping("fileDictByName")
    public Object fileDictByName(@RequestParam String fileName){
        /* 获取文件夹路径 */
        String filePath = "D:\\file\\"+fileName;
        List<String> list = getDataByFileName(filePath);
        List<FileVo> fileVos = new ArrayList<>();
        int i = 0;
        for (String str: list) {
            FileVo fileVo = new FileVo();
            fileVo.setIndex(i++);
            fileVo.setFilename(str);
            fileVos.add(fileVo);
        }
        return fileVos;
    }

    /* txt文件的创建 */
    public boolean createFile(String filePath){
        File file = new File(filePath);
        boolean staut = false;
        try {
            staut = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (staut){
            System.out.println("创建成功！");
        }else{
            System.out.println("文件已存在！");
        }
        return staut;
    }

    /* 换行向txt文件中写入数据
     * 写入的时候会覆盖掉之前的数据
     *  */
    public void writeToFile(String filePath,List<String> list){
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
            for (String str:list) {
                out.write(str + "\n"); //写入时候换行
            }
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /* txt文件的删除 */
    public boolean delFile(String pathName){
        File file = new File(pathName);
        boolean staut = file.delete();
        if (staut){
            System.out.println("删除成功");
        }else{
            System.out.println("需要删除的文件不存在");
        }
        return staut;
    }

    /* 读取目录中所有文件的名字 */
    public List<String> getAllFileNameByDir(String dirPath){
        File dir = new File(dirPath);
        File[] file = dir.listFiles();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < file.length; i++){
            list.add(file[i].getName());
        }
        return list;
    }

    /* 读入文件中的内容 */
    public List<String> getDataByFileName(String filePath){
        List<String> list = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(filePath));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                if (StringUtils.isNotBlank(s)) {
                    list.add(s);
                }
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<String> model(){
        List<String> list = new ArrayList<>();
        list.add("文件1");
        list.add("文件2");
        list.add("文件3");
        return list;
    }
}

